<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <title>科协视频检测系统</title>
    <meta name="keywords" content="科协视频检测系统">
    <meta name="description" content="科协视频检测系统">
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css" rel="stylesheet">
    <link href="/css/login.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html"/>
    <![endif]-->
    <script>
        if (window.top !== window.self) {
            window.top.location = window.location;
        }
    </script>

</head>

<body class="signin">
<div class="signinpanel">
    <div class="row">
        <div style="width: 360px; margin-left: 62%;margin-top: 15%;">
            <form id="signupForm">
                <h3 class="text-center">用户登录</h3>
                <p class="m-t-md text-center">欢迎登录科协视频检测后台管理系统</p>
                <input  name="username" class="form-control uname"/>
                <input type="password" name="password" class="form-control pword m-b"/>
                <div class="m-b" style="position: relative;">
                    <input id="IDcode" class="form-control" name="IDcode" placeholder="验证码" style="width:170px;margin-top: 0px;" required="required"/>
                    <img th:src="@{/defaultKaptcha}" style="width: 110px;position: absolute;top: 0;right: 0;height: 34px;" alt="验证码" title="点击刷新验证码" id="IDcodeImg"/>
                </div>
                <a id="login" class="btn btn-login btn-block">登录</a>

            </form>
        </div>
    </div>
</div>
<script th:inline="javascript"> var ctx = [[@{/}]] ; </script>
<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4" th:src="@{/js/jquery.min.js?v=2.1.4}"></script>
<script src="/js/bootstrap.min.js?v=3.3.6" th:src="@{/js/bootstrap.min.js?v=3.3.6}"></script>

<!-- 自定义js -->
<script src="/js/content.js?v=1.0.0" th:src="@{/js/content.js?v=1.0.0}"></script>

<!-- jQuery Validation plugin javascript-->
<script src="/js/ajax-util.js"></script>
<script src="/js/plugins/validate/jquery.validate.min.js" th:src="@{/js/plugins/validate/jquery.validate.min.js}"></script>
<script src="/js/plugins/validate/messages_zh.min.js" th:src="@{/js/plugins/validate/messages_zh.min.js}"></script>
<script src="/js/plugins/layer/layer.min.js" th:src="@{/js/plugins/layer/layer.min.js}"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#login").on('click',function(){$("#signupForm").submit();});
        validateRule();
        // getCode ()
        $('#IDcodeImg').click(function(){
            this.src = '/defaultKaptcha?d='+new Date()*1
            $('#IDcode').val('');
            
        })
    });

    $.validator.setDefaults({
        submitHandler: function () {
            login();
        }
    });

    function login() {
        $.ajax({
            type: "POST",
            url: ctx+"login",
            data: $('#signupForm').serialize(),
            success: function (r) {
                 if (r.code == 0) {
                    var index = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    parent.location.href = '/index';
                } else {
                    layer.msg(r.msg);
                }
            },
        });
        console.log($('#IDcode').val())
    }
    $('#signupForm input').on('keypress', function(event){
        if(event.keyCode === 13) {
            $("#signupForm").submit();
            validateRule()
        }
    })

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                },
                IDcode: {
                    required: true,
                    // IDcodeVer: true,
                    checkCode:true
                }
            },
            messages: {
                username: {
                    required: icon + "请输入您的用户名",
                },
                password: {
                    required: icon + "请输入您的密码",
                },
                IDcode: {
                    // IDcode: icon + "请输入验证码",
                    checkCode: icon + "请输入正确的验证码",
                }
            }
        })
    }


function checkCode(value) {
    var deferred = $.Deferred();//延迟方法
    $.ajax({
        type:"get",
        url:"/verifyDefaultKaptcha/" + value,
        async:false,
         dataType: "json",
         success: function(data, type) {

             if(data){
                 deferred.resolve();
               }else{
                  deferred.reject();
               }    
          }
    });
    //deferred.state()有3个状态:pending:还未结束,rejected:失败,resolved:成功
    return deferred.state() == "resolved" ? true : false;
}   
jQuery.validator.addMethod("checkCode", function(value, element) {
    return checkCode(value);
},'验证码错误');        
</script>
</body>
</html>
